package com.itcast.flink.screen.database;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.itcast.flink.screen.database.bo.HotOrder;
import com.itcast.flink.screen.database.bo.Order;
import com.itcast.flink.screen.database.json.GsonConvertUtil;
import com.itcast.flink.screen.database.repository.HotOrderRepository;
import com.itcast.flink.screen.database.spring.ApplicationContextUtil;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.util.Collector;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.*;

/**
 * <p>Description: </p>
 * @date 
 * @author 
 * @version 1.0
 * <p>Copyright:Copyright(c)2020</p>
 */
@SpringBootApplication
@ComponentScan(basePackages = {"com.itcast"})
@EnableTransactionManagement
public class ScreenDatabaseApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(ScreenDatabaseApplication.class);
        application.setBannerMode(Banner.Mode.OFF);
        application.run(args);

    }

    /**
     * 执行Flink任务处理
     * @throws Exception
     */
    private void executeFlinkTask() throws Exception {

        // 1. 创建运行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 2. 设置kafka服务连接信息
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "10.10.20.132:9092");
        properties.setProperty("group.id", "fink_group");

        // 3. 创建Kafka消费端
        FlinkKafkaConsumer kafkaProducer = new FlinkKafkaConsumer(
                "order_binlog",                  // 目标 topic
                new SimpleStringSchema(),   // 序列化 配置
                properties);

        // 调试，重新从最早记录消费
        kafkaProducer.setStartFromEarliest();     // 尽可能从最早的记录开始
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
        env.setParallelism(1);

        // 4. 读取Kafka数据源
        DataStreamSource<String> socketStr = env.addSource(kafkaProducer);

        // 5. 数据过滤转换处理
        socketStr.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String value) throws Exception {
                JsonObject jsonObject = GsonConvertUtil.getSingleton().getJsonObject(value);
                String isDDL = jsonObject.get("isDdl").getAsString();
                String type = jsonObject.get("type").getAsString();
                // 过滤条件： 非DDL操作， 并且是新增的数据
                return isDDL.equalsIgnoreCase("false") && "INSERT".equalsIgnoreCase(type);
            }
        }).flatMap(new FlatMapFunction<String, Order>() {
            @Override
            public void flatMap(String value, Collector<Order> out) throws Exception {
                // 获取JSON中的data数据
                JsonArray dataArray = GsonConvertUtil.getSingleton().getJsonObject(value).getAsJsonArray("data");
                // 将data数据转换为java对象
                for(int i =0; i< dataArray.size(); i++) {
                    JsonObject jsonObject = dataArray.get(i).getAsJsonObject();
                    Order order = GsonConvertUtil.getSingleton().cvtJson2Obj(jsonObject, Order.class);
                    System.out.println("order => " + order);
                    out.collect(order);
                }
            }
        })
        .assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor<Order>(Time.seconds(0)) {
            @Override
            public long extractTimestamp(Order element) {
                return element.getExecTime();
            }
        })
        .keyBy(Order::getGoodsId)
        .timeWindow(Time.hours(24), Time.seconds(3))
        .aggregate(new TotalAmount(), new AmountWindow())
        .keyBy(HotOrder::getTimeWindow)
        .process(new TopNHotOrder());

        // 6. 执行任务
        env.execute("job");
    }

    @Override
    public void run(String... args) throws Exception {
        // 运行Flink任务
        executeFlinkTask();
    }

    private  static class TotalAmount implements AggregateFunction<Order, Order, Order> {
        @Override
        public Order createAccumulator() {
            Order order = new Order();
            order.setTotalAmount(0l);
            return order;
        }

        @Override
        public Order add(Order value, Order accumulator) {
            accumulator.setGoodsId(value.getGoodsId());
            accumulator.setGoodsName((value.getGoodsName()));
            accumulator.setTotalAmount(accumulator.getTotalAmount() + (value.getExecPrice() * value.getExecVolume()));
            return accumulator;
        }

        @Override
        public Order getResult(Order accumulator) {
            return accumulator;
        }

        @Override
        public Order merge(Order a, Order b) {
            return null;
        }
    }

    /**
     * 热销商品， 时间窗口对象转换处理
     */
    private static class AmountWindow implements WindowFunction<Order, HotOrder, Long, TimeWindow> {

        @Override
        public void apply(Long goodsId, TimeWindow window, Iterable<Order> input, Collector<HotOrder> out) throws Exception {
            Order order = input.iterator().next();
            out.collect(new HotOrder(goodsId, order.getGoodsName(), order.getTotalAmount(), window.getEnd()));
        }
    }

    /**
     * 热销商品的统计排行实现
     */
    private class TopNHotOrder extends KeyedProcessFunction<Long, HotOrder, String> {

        private ListState<HotOrder> orderState;

        @Override
        public void processElement(HotOrder value, Context ctx, Collector<String> out) throws Exception {
            // 将数据加入到状态列表里面
            orderState.add(value);
            // 注册定时器
            ctx.timerService().registerEventTimeTimer(value.getTimeWindow());
        }

        @Override
        public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
            List<HotOrder> orderList = new ArrayList<>();
            for(HotOrder order : orderState.get()){
                orderList.add(order);
            }
            // 按照成交总金额， 倒序排列
            orderList.sort(Comparator.comparing(HotOrder::getTotalAmount).reversed());
            orderState.clear();
            // 将数据写入至ES
            HotOrderRepository hotOrderRepository = (HotOrderRepository) ApplicationContextUtil.getBean("hotOrderRepository");
            StringBuffer strBuf = new StringBuffer();
            for(HotOrder order: orderList) {
                order.setId(order.getGoodsId());
                order.setCreateDate(new Date(order.getTimeWindow()));
                hotOrderRepository.save(order);
                strBuf.append(order).append("\n");
                System.out.println("result => " + order);
            }
            out.collect(strBuf.toString());
        }

        @Override
        public void open(Configuration parameters) throws Exception {
            super.open(parameters);
            orderState = getRuntimeContext().getListState(new ListStateDescriptor<HotOrder>("hot-order", HotOrder.class));

        }
    }
}
